package hflyeer.gitee.com.controller;

import hflyeer.gitee.com.entity.Department;
import hflyeer.gitee.com.entity.Employee;
import hflyeer.gitee.com.service.DepartmentService;
import hflyeer.gitee.com.service.EmployeeService;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Controller("employeeController")
public class EmployeeController {

    @Resource(name = "employeeService")
    private EmployeeService employeeService;

    @Resource(name = "departmentService")
    private DepartmentService departmentService;

    /**
     * /employee/list.do
     * 显示员工列表
     *
     * @param request
     * @param response
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Employee> employees = employeeService.find();
        request.setAttribute("LIST",employees);
        request.getRequestDispatcher("../employee_list.jsp").forward(request,response);
    }

    /**
     * /employee/toAdd.do
     * 跳转到员工添加界面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Department> departments = departmentService.find();
        request.getSession().setAttribute("depList",departments);
        response.sendRedirect(request.getContextPath()+"/employee_add.jsp");
    }

    /**
     * /employee/add.do
     * 添加员工
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        request.getSession().removeAttribute("depList");
        Map<String, String[]> parameter = request.getParameterMap();
        Employee employee = new Employee();
        employee.setAccount(parameter.get("account")[0]);
        employee.setPassword(parameter.get("password")[0]);
        employee.setStatus(parameter.get("status")[0]);
        employee.setDepartmentId(Integer.parseInt(parameter.get("department")[0]));
        employee.setName(parameter.get("name")[0]);
        employee.setSex(parameter.get("sex")[0]);
        employee.setIdCart(parameter.get("id_cart")[0]);
        employee.setJoinTime(new SimpleDateFormat("yyyy-MM-dd").parse(parameter.get("join_time")[0]));
        employee.setOutTime(new SimpleDateFormat("yyyy-MM-dd").parse(parameter.get("out_time")[0]));
        employee.setBirth(new SimpleDateFormat("yyyy-MM-dd").parse(parameter.get("birth")[0]));
        employee.setExtension(parameter.get("extension")[0]);
        employeeService.add(employee);
        response.sendRedirect(request.getContextPath()+"/employee/list.do");
    }

    /**
     * /employee/toEdit.do
     * 跳转到员工修改界面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Employee employeeServiceOne = employeeService.findOne(id);
        request.getSession().setAttribute("employee",employeeServiceOne);
        List<Department> departmentList = departmentService.find();
        request.getSession().setAttribute("depList",departmentList);
        response.sendRedirect(request.getContextPath()+"/employee_edit.jsp");
    }

    /**
     * /employee/edit.do
     * 修改员工
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        request.getSession().removeAttribute("employee");
        Map<String, String[]> parameter = request.getParameterMap();
        Employee employee = new Employee();
        employee.setId(Integer.parseInt(parameter.get("id")[0]));
        employee.setAccount(parameter.get("account")[0]);
        employee.setPassword(parameter.get("password")[0]);
        employee.setStatus(parameter.get("status")[0]);
        employee.setDepartmentId(Integer.parseInt(parameter.get("department")[0]));
        employee.setName(parameter.get("name")[0]);
        employee.setSex(parameter.get("sex")[0]);
        employee.setIdCart(parameter.get("id_cart")[0]);
        employee.setJoinTime(new SimpleDateFormat("yyyy-MM-dd").parse(parameter.get("join_time")[0]));
        employee.setOutTime(new SimpleDateFormat("yyyy-MM-dd").parse(parameter.get("out_time")[0]));
        employee.setBirth(new SimpleDateFormat("yyyy-MM-dd").parse(parameter.get("birth")[0]));
        employee.setExtension(parameter.get("extension")[0]);
        employeeService.edit(employee);
        response.sendRedirect(request.getContextPath()+"/employee/list.do");
    }

    /**
     * /employee/remove.do
     * 删除员工
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        employeeService.remove(id);
        response.sendRedirect(request.getContextPath()+"/employee/list.do");
    }

    /**
     * /employee/detail.do
     * 员工详情
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Employee e = employeeService.findOne(id);
        request.getSession().setAttribute("employeeDetail",e);
        response.sendRedirect(request.getContextPath()+"/employee_detail.jsp");
    }
}
